package com.zhuozhou.jdyc.base;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.databinding.BaseSearchTitleActivityBinding;

public abstract class BaseSearchActivity<VM extends BaseViewModel> extends BaseActivity<VM, BaseSearchTitleActivityBinding> {
    private Fragment mCurrentFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.base_search_title_activity;
    }

    /**
     * 给search添加子菜单
     * @param hint item提示文字
     * @param onClickListener 点击事件回调
     */
    public void addMenuView(String hint,View.OnClickListener onClickListener) {
        View view = LayoutInflater.from(this).inflate(R.layout.option_item, null);
        binding.baseSearchView.llMenuOption.addView(view);
        TextView tvOptionMenu = view.findViewById(R.id.tv_option_menu);
        tvOptionMenu.setHint(hint);
        tvOptionMenu.setOnClickListener(onClickListener);
    }

    public void setFragment(FragmentManager mFragmentManager, Fragment fragment, @IdRes int id) {
        if (fragment == null) {
            return;
        }
        if (mCurrentFragment != fragment) {
            if (fragment.isAdded()) {
                mFragmentManager.beginTransaction().hide(mCurrentFragment).show(fragment).commit();
            } else {
                mFragmentManager.beginTransaction().replace(id, fragment).commit();
            }
            mCurrentFragment = fragment;
        }
    }

}
